package com.yonyougov.devops.cruxcisupport.rancher;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyougov.devops.cruxcisupport.rancher.request.DeployAllByEnvNameReq;
import com.yonyougov.devops.cruxcisupport.rancher.vo.CreateClusterRequestVo;
import com.yonyougov.devops.cruxcisupport.rancher.vo.CreatePodRequestVo;
import com.yonyougov.devops.cruxcisupport.rancher.vo.GetCmdToAddNodesRequestVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping(value = "/api/v2/rancher")
public class RancherController {
    private final RancherService rancherService;
    private final ObjectMapper objectMapper;

    public RancherController(RancherService rancherService, ObjectMapper objectMapper) {
        this.rancherService = rancherService;
        this.objectMapper = objectMapper;
    }


    @PostMapping("/cluster")
    public boolean createCluster(@RequestBody @Valid CreateClusterRequestVo request) {
        return rancherService.createCluster(request);
    }

    @PostMapping("/cluster/nodes/token")
    public String getCmdToAddNodes(@RequestBody @Valid GetCmdToAddNodesRequestVo request) {
        return rancherService.getCmdToAddNodes(request);
    }

    @PostMapping("/pod")
    @SneakyThrows
    public boolean createPod(@RequestBody String request) {
        try {
            CreatePodRequestVo createPodRequestVo = objectMapper.readValue(request, CreatePodRequestVo.class);
            //CreatePodRequestVo createPodRequestVo = JSONObject.parseObject(request, CreatePodRequestVo.class);
            String envName = createPodRequestVo.getEnvName();
            String image = createPodRequestVo.getImage();
            String packageVersion = createPodRequestVo.getPackageVersion();
            if (StringUtils.isBlank(envName)) {
                log.info("envName : {}  is empty or null", envName);
                return false;
            }
            if (StringUtils.isBlank(image) && StringUtils.isBlank(packageVersion)) {
                log.info("packageVersion : {} or image : {} is empty or null", packageVersion, image);
                return false;
            }
            rancherService.createPod(createPodRequestVo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("catch exception when createPod is {}", e.getMessage());
        }
        return true;
    }

    @PostMapping(value = "/pods/all")
    public boolean deployAllPodByEnvName(@RequestBody @Valid DeployAllByEnvNameReq request) {
        String envName = request.getEnvName();
        log.info("invoke deployAllPodByEnvName method,param: envName is : {}", envName);
        return rancherService.deployAllPodByEnvName(envName);
    }
}
